package com.file.manage;

import android.content.Context;
import android.os.StatFs;

import java.io.File;

public class FileUtil {

    /**
     * 二維数组
     *
     * @Fields MIME_MapTable : MIME TYPE映射表
     */
    private static final String[][] MIME_MapTable = {
            // {后缀名， MIME类型}
            {".3gp", "video/3gpp"},
            {".apk", "application/vnd.android.package-archive"},
            {".asf", "video/x-ms-asf"},
            {".avi", "video/x-msvideo"},
            {".bin", "application/octet-stream"},
            {".bmp", "image/bmp"},
            {".c", "text/plain"},
            {".class", "application/octet-stream"},
            {".conf", "text/plain"},
            {".cpp", "text/plain"},
            {".doc", "application/msword"},
            {".docx",
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
            {".xls", "application/vnd.ms-excel"},
            {".xlsx",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
            {".exe", "application/octet-stream"},
            {".gif", "image/gif"},
            {".gtar", "application/x-gtar"},
            {".gz", "application/x-gzip"},
            {".h", "text/plain"},
            {".htm", "text/html"},
            {".html", "text/html"},
            {".jar", "application/java-archive"},
            {".java", "text/plain"},
            {".jpeg", "image/jpeg"},
            {".jpg", "image/jpeg"},
            {".js", "application/x-javascript"},
            {".log", "text/plain"},
            {".m3u", "audio/x-mpegurl"},
            {".m4a", "audio/mp4a-latm"},
            {".m4b", "audio/mp4a-latm"},
            {".m4p", "audio/mp4a-latm"},
            {".m4u", "video/vnd.mpegurl"},
            {".m4v", "video/x-m4v"},
            {".mov", "video/quicktime"},
            {".mp2", "audio/x-mpeg"},
            {".mp3", "audio/x-mpeg"},
            {".mp4", "video/mp4"},
            {".mpc", "application/vnd.mpohun.certificate"},
            {".mpe", "video/mpeg"},
            {".mpeg", "video/mpeg"},
            {".mpg", "video/mpeg"},
            {".mpg4", "video/mp4"},
            {".mpga", "audio/mpeg"},
            {".msg", "application/vnd.ms-outlook"},
            {".ogg", "audio/ogg"},
            {".pdf", "application/pdf"},
            {".png", "image/png"},
            {".pps", "application/vnd.ms-powerpoint"},
            {".ppt", "application/vnd.ms-powerpoint"},
            {".pptx",
                    "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
            {".prop", "text/plain"}, {".rc", "text/plain"},
            {".rmvb", "audio/x-pn-realaudio"}, {".rtf", "application/rtf"},
            {".sh", "text/plain"}, {".tar", "application/x-tar"},
            {".tgz", "application/x-compressed"}, {".txt", "text/plain"},
            {".wav", "audio/x-wav"}, {".wma", "audio/x-ms-wma"},
            {".wmv", "audio/x-ms-wmv"},
            {".wps", "application/vnd.ms-works"}, {".xml", "text/plain"},
            {".z", "application/x-compress"},
            {".zip", "application/x-zip-compressed"}, {"", "*/*"}};

    private static final String DOC = ".doc|.docx";

    private static final String XLS = ".xls|.xlsx";

    private static final String PDF = ".pdf";

    private static final String AUDIO = ".m3u|.m4b|.m4p|.mp2|.mp3|.mpga|.ogg|.wav|.wma";

    private static final String VIDEO = ".asf|.avi|.m4u|.m4v|.mov|.mp4|.mpe|.mpg4|.rmvb";

    private static final String PPT = ".ppt|.pptx|.pps";

    private static final String PIC = ".bmp|.gif|.jpeg|.jpg|.png";

    private static final String TXT = ".c|.conf|.cpp|.h|.html|.java|.log|.prop|.rc|.sh|.txt.xml";

    private static final String APK = ".apk";

    private static final String ZIP = ".zip|.rar";

    /**
     * @param fileName
     * @return String
     * @Title: getMIMEType
     * @Description: 获取当前文件的MIME-TYPE
     */
    public static String getMIMEType(String fileName) {

        String type = "*/*";
        // String fName = file.getName();
        // 获取后缀名前的分隔符"."在fName中的位置。
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex < 0) {
            return type;
        }
        /* 获取文件的后缀名 */
        String end = fileName.substring(dotIndex, fileName.length())
                .toLowerCase();
        if (end == "")
            return type;
        // 在MIME和文件类型的匹配表中找到对应的MIME类型。
        for (int i = 0; i < MIME_MapTable.length; i++) {
            if (end.equals(MIME_MapTable[i][0]))
                type = MIME_MapTable[i][1];
        }
        return type;
    }

    /**
     * @return String
     * @Title: getMIMEType
     * @Description: 获取当前文件的MIME-TYPE
     */
    public static String getMIMEType(File file) {
        if (file == null)
            throw new NullPointerException("the argument 'file' is null");
        String fileName = file.getName();
        return getMIMEType(fileName);
    }

    /**
     * @param fileName
     * @return String
     * @Title: getFileSuffix
     * @Description: 获取文件后缀
     */
    public static String getFileSuffix(String fileName) {
        if ("".equals(fileName) || fileName == null)
            throw new NullPointerException("fileName is null");
        if (fileName.lastIndexOf(".") != -1)
            return fileName.substring(fileName.lastIndexOf("."));
        return null;
    }

    /**
     * @param suffix  : 文件后缀名
     * @param context : 系统上下文环境
     * @return int
     * @Title: getFileTypeDrawable
     * @Description: 通过文件后缀名，获取该文件所对应的图片
     */
    public static int getFileTypeDrawable(String suffix, Context context) {
        suffix = suffix.toLowerCase();
        if (DOC.contains(suffix)) {
            return R.drawable.icon_file_doc;
        } else if (AUDIO.contains(suffix)) {
            return R.drawable.icon_file_music;
        } else if (APK.contains(suffix)) {
            return R.drawable.icon_file_apk;
        } else if (PDF.contains(suffix)) {
            return R.drawable.icon_file_pdf;
        } else if (PIC.contains(suffix)) {
            return R.drawable.icon_file_pic;
        } else if (PPT.contains(suffix)) {
            return R.drawable.icon_file_ppt;
        } else if (VIDEO.contains(suffix)) {
            return R.drawable.icon_file_video;
        } else if (XLS.contains(suffix)) {
            return R.drawable.icon_file_xls;
        } else if (ZIP.contains(suffix)) {
            return R.drawable.icon_file_zip;
        } else if (TXT.contains(suffix)) {
            return R.drawable.icon_file_txt;
        } else
            return R.drawable.icon_file_other;
    }

    /**
     * @return boolean
     * @Title: ExistSDCard
     * @Description: 判断SD卡是否存在
     */
    public static boolean ExistSDCard() {
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            return true;
        } else
            return false;
    }

    /**
     * @return long
     * @Title: getSDFreeSize
     * @Description: 获取Sd卡剩余容量
     */
    public static long getSDFreeSize() {
        //取得SD卡文件路径
        File path = android.os.Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        //获取单个数据块的大小(Byte)
        long blockSize = sf.getBlockSize();
        //空闲的数据块的数量
        long freeBlocks = sf.getAvailableBlocks();
        //返回SD卡空闲大小
        return (freeBlocks * blockSize) / 1024 / 1024; //单位MB
    }

    /**
     * @return long
     * @Title: getSDAllSize
     * @Description: 获取SD卡总容量
     */
    public long getSDAllSize() {
        //取得SD卡文件路径
        File path = android.os.Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        //获取单个数据块的大小(Byte)
        long blockSize = sf.getBlockSize();
        //获取所有数据块数
        long allBlocks = sf.getBlockCount();
        //返回SD卡大小
        return (allBlocks * blockSize) / 1024 / 1024; //单位MB
    }
}
